var ports = require("../../utils/ports.js")
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {},
  onLoad(e) {
    var type = e.type
    if (type) {
      this.setData({
        type
      })
    }
  },
  onShow: function (options) {
    this.getMemberAddressList()
  },
  //地址列表
  getMemberAddressList() {
    var that = this
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      mask: true
    })
    var req = {}
    req.ListName = 'AddressList'
    ports.ModuleMemberAddress.getMemberAddressList(req, that, res => {
      wx.hideToast()
    })
  },
  //微信地址
  wxAddress: function () {
    wx.chooseAddress({
      success: function (res) {
        console.info(res)
      }
    })
  },
  //新增地址
  toAddAddress:function() {
    wx.navigateTo({
      url: '/pages/editAddress/editAddress?title=add',
    })
  },
  //编辑地址
  toUpdateAddress(e) {
    wx.navigateTo({
      url: '/pages/editAddress/editAddress?title=update&id=' + e.currentTarget.dataset.id,
    })
  },
  onPullDownRefresh: function () {
    var that = this;
    ports.ModuleAll.PullDownRefresh(that, () => {
      that.getMemberAddressList();
    })
  },
  onReachBottom: function () {
    var that = this;
    ports.ModuleAll.ReachBottom('AddressList', that, res => {
      that.getMemberAddressList();
    })
  },
  //订单选择地址
  selectAddress:function(e) {
    var that = this
    if (that.data.type) {
      var index = e.currentTarget.dataset.index
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2];
      prevPage.setData({
        address: that.data.AddressList[index],
      })
      wx.navigateBack();
    }
  }
})